package com.supermarket.product.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.supermarket.common.core.domain.R;
import com.supermarket.common.core.utils.StringUtils;
import com.supermarket.product.domain.ProductAttribute;
import com.supermarket.product.domain.ProductType;
import com.supermarket.product.domain.dto.ProductGoodsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.supermarket.common.log.annotation.Log;
import com.supermarket.common.log.enums.BusinessType;
import com.supermarket.common.security.annotation.RequiresPermissions;
import com.supermarket.product.domain.ProductGoods;
import com.supermarket.product.service.IProductGoodsService;
import com.supermarket.common.core.web.controller.BaseController;
import com.supermarket.common.core.web.domain.AjaxResult;
import com.supermarket.common.core.utils.poi.ExcelUtil;
import com.supermarket.common.core.web.page.TableDataInfo;

/**
 * 商品管理Controller
 * 
 * @author      
 * @date 2025-07-20
 */
@RestController
@RequestMapping("/products")
public class ProductGoodsController extends BaseController
{
    @Autowired
    private IProductGoodsService productGoodsService;

    /**
     * 查询商品管理列表
     */
    @RequiresPermissions("product:products:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductGoods productGoods)
    {
        startPage();
        List<ProductGoods> list = productGoodsService.selectProductGoodsList(productGoods);
        return getDataTable(list);
    }

    /**
     * app端查询商品列表
     * @param productGoods
     * @return
     */
    @GetMapping("/getList")
    public TableDataInfo getList(ProductGoods productGoods)
    {
        startPage();
        List<ProductGoods> list = productGoodsService.selectProductGoodsList(productGoods);
        return getDataTable(list);
    }
    /**
     * 查询商品管理列表远程调用
     */
    @GetMapping("/getGoodsList")
    public R<List<ProductGoods>> getGoodsList( )
    {
        List<ProductGoods> list = productGoodsService.getGoodsList();
        return R.ok(list);
    }

    /**
     * 获取商品类型下拉
     */
    @GetMapping("/getTypeList")
    public AjaxResult getTypeList( )
    {
      List<ProductType> list=  productGoodsService.getTypeList();
      return AjaxResult.success(list);
    }
    /**
     * 获取选择属性下拉
     */
    @GetMapping("/getAttributeNameListAll")
    public AjaxResult getAttributeNameListAll( )
    {
        List<ProductAttribute> list=  productGoodsService.getAttributeNameListAll();
        return AjaxResult.success(list);
    }

    /**
     * 导出商品管理列表
     */
    @RequiresPermissions("product:products:export")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductGoods productGoods)
    {
        List<ProductGoods> list = productGoodsService.selectProductGoodsList(productGoods);
        ExcelUtil<ProductGoods> util = new ExcelUtil<ProductGoods>(ProductGoods.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @RequiresPermissions("product:products:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productGoodsService.selectProductGoodsById(id));
    }

    /**
     * 新增商品管理
     */
    @RequiresPermissions("product:products:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductGoodsDto productGoodsVo)
    {
        //获取商品名称
        String goodsName = productGoodsVo.getGoodsName();
        //调用校验方法
        validateGoodsName(goodsName);
        return toAjax(productGoodsService.insertProductGoods(productGoodsVo));
    }

    /**
     *  校验规则
     * 商品名称必填
     * 最多60个字符
     * 最多30个汉字
     */
    public void validateGoodsName(String goodsName) {
        if (StringUtils.isEmpty(goodsName)) {
            throw new IllegalArgumentException("请输入商品名称，最多支持60个字符，30个汉字。");
        }
        if (goodsName.length() > 60) {
            throw new IllegalArgumentException("请输入商品名称，最多支持60个字符，30个汉字。");
        }
        int chineseCount = (int) goodsName.chars()
                .filter(c -> c >= 0x4E00 && c <= 0x9FA5)
                .count();
        if (chineseCount > 30) {
            throw new IllegalArgumentException("请输入商品名称，最多支持60个字符，30个汉字。");
        }
    }

    /**
     * 修改商品管理
     */
    @RequiresPermissions("product:products:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductGoodsDto productGoodsVo)
    {
        //获取商品名称
        String goodsName = productGoodsVo.getGoodsName();
        //调用校验方法
        validateGoodsName(goodsName);
        return toAjax(productGoodsService.updateProductGoods(productGoodsVo));
    }

    /**
     * 删除商品管理
     */
    @RequiresPermissions("product:products:remove")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productGoodsService.deleteProductGoodsByIds(ids));
    }
}
